﻿namespace Cos.Core.NHibernate.Tests
{
    using FluentNHibernate;
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;

    using global::NHibernate;

    using NUnit.Framework;

    public class FixtureBase
    {
        protected SessionSource SessionSource { get; set; }

        protected ISession Session { get; private set; }

        protected PersistenceModel model;

        public FixtureBase(PersistenceModel model)
        {
            this.model = model;
        }

        [TestFixtureSetUp]
        public void SetupContext()
        {
            var cfg = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                    .ConnectionString(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\FullDatabase.mdf;Integrated Security=True;User Instance=True").ShowSql());

            SessionSource = new SessionSource(cfg.BuildConfiguration().Properties, this.model);
            this.Session = this.SessionSource.CreateSession();
            this.SessionSource.Configuration.BuildSessionFactory();
        }

        [TestFixtureTearDown]
        public void TearDownContext()
        {
            this.Session.Close();
            this.Session.Dispose();
        }
    }
}